package com.lsp.microuser.point.service.impl;

import com.lsp.microcommon.constants.PointTypeEum;
import com.lsp.microcommon.entity.Point;
import com.lsp.microcommon.entity.PointRule;
import com.lsp.microcommon.utils.UserUtils;
import com.lsp.microuser.point.dao.PointDao;
import com.lsp.microuser.point.service.PointRuleService;
import com.lsp.microuser.point.service.PointService;
import com.lsp.microuser.user.service.UserService;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PointServiceImpl implements PointService {

    @Resource
    private PointDao pointDao;

    @Resource
    @Lazy
    private UserService userService;

    @Resource
    private PointRuleService pointRuleService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void addPoint(Point point) {

//        Integer uid = UserUtils.getUser();
//        userService.updatePoint(uid,point.getPoint());
        userService.updatePoint(point.getUid(),point.getPoint());
        pointDao.insert(point);
    }

    @Override
    public List<Point> pointChangeList() {
        Integer uid = UserUtils.getUser();
        return pointDao.selectListByUid(uid);
    }

    @Override
    public void addArticlePoint() {


        List<PointRule> pointRules = pointRuleService.selectPointRuleList(PointTypeEum.TYPE_ARTICLE.getType());

        if (pointRules != null && pointRules.size() > 0) {
            Point point = new Point();
            PointRule pointRule = pointRules.get(0);

            point.setPointType(PointTypeEum.TYPE_ARTICLE.getType());
            point.setUid(UserUtils.getUser());
            point.setPoint(pointRule.getPoint());

            this.addPoint(point);
        }
    }
}
